using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHeroInfo : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIHeroInfo),
            layer = EUILayer.Normal,
            isFullScreen = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIHeroInfo
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton buttonHeroList { protected set; get; }
            public GameObject topLeftBack { protected set; get; }
            public GameObject toggleTab { protected set; get; }
            public GameObject overview { protected set; get; }
            public GameObject heroWarehouse { protected set; get; }
            public GameObject attrDetailPanel { protected set; get; }
            public UnityEngine.RectTransform panel { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonHeroList", out var __tbv0);
                this.buttonHeroList = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv1);
                this.topLeftBack = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleTab", out var __tbv2);
                this.toggleTab = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("overview", out var __tbv3);
                this.overview = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("heroWarehouse", out var __tbv4);
                this.heroWarehouse = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("attrDetailPanel", out var __tbv5);
                this.attrDetailPanel = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("panel", out var __tbv6);
                this.panel = __tbv6;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private ListScrollAdapter<UIN_HeroInfoTab> tabs = null;
        private UIN_HeroInfoOverview heroInfoOverview = null;
        private UIN_HeroInfoWarehouse heroInfoWarehouse = null;
        private UIN_HeroInfoAttrDetail heroInfoAttrDetail = null;
        private UIBaseNode currRight = null;

        private UD_HeroInfo uiData => _data as UD_HeroInfo;
        private HeroData heroData = null;

        protected UIB_UIHeroInfo ui { get; set; }
        private UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIHeroInfo();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void AddMsgListeners()
        {
            uiMsgDispatcher.AddListener<HeroData>(MsgEventType.HeroInfoRefreshByChangeHero, this.OnMsgRefreshOnChangeHero);
            uiMsgDispatcher.AddListener(MsgEventType.HeroInfoShowAttrDetails, this.OnMsgShowHeroAttrDetails);
        }

        private void OnMsgRefreshOnChangeHero(HeroData heroData)
        {
            this.uiData.UpdateHeroData(heroData);
            this.Refresh(this.uiData);
        }

        private void OnMsgShowHeroAttrDetails()
        {
            this.heroInfoAttrDetail.Show(this.heroData);
        }

        protected override void OnDispose()
        {
            this.heroData = null;
        }

        protected override void OnInit()
        {
            this.tabs = this.InitListScroll<UIN_HeroInfoTab>(this.ui.toggleTab, this.OnTabChanged);
            this.tabs.InitUnitSelected(new ListUnitSelect<UIN_HeroInfoTab>.UnitSelectData()
            {
                selectedType = UnitSelectType.ByExToggle,
                callSelected = this.OnTabSelected,
                callCheckIsCanSelected = this.CheckIsCanSelectTab,
            });

            this.heroInfoOverview = this.InitChildNode<UIN_HeroInfoOverview>(this.ui.overview);
            this.heroInfoWarehouse = this.InitChildNode<UIN_HeroInfoWarehouse>(this.ui.heroWarehouse);
            this.heroInfoWarehouse.Hide();
            this.heroInfoAttrDetail = this.InitChildNode<UIN_HeroInfoAttrDetail>(this.ui.attrDetailPanel);
            this.heroInfoAttrDetail.Hide();
            this.ui.buttonHeroList.onClick.AddListener(this.OnClickHeroWarehouse);
            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow(UIBaseData data)
        {
            Refresh(data as UD_HeroInfo);
        }

        protected override UIBaseData OnGetLeaveData()
        {
            var d = UD_HeroInfo.NewFromPool();
            d.Sync(uiData.heroData, uiData.isShowList, uiData.indexTab);
            return d;
        }

        private void Refresh(UD_HeroInfo uiData)
        {
            if (uiData == null)
            {
                LogGame.LogError("uiData不能为空。");
                return;
            }
            this.heroData = uiData.heroData;

            this.ui.buttonHeroList.SetActive(this.uiData.isShowList);
            int cnt = heroData.isGeted ? (int)UIHeroInfoTabType.MaxLength : 1;
            int indexTab = this.uiData.indexTab;
            int indexSelected = (indexTab == -1 || indexTab >= cnt) ? 0 : indexTab;
            this.tabs.RefillCells(cnt, indexSelected);
        }

        private void OnClickHeroWarehouse()
        {
            this.heroInfoWarehouse.Show(this.heroData);
        }

        private void OnTabChanged(UIN_HeroInfoTab item, int index)
        {
            item.Show(index);
        }

        private void OnTabSelected(int index, UIN_HeroInfoTab item)
        {
            if (index == -1)
                return;
            this.uiData.UpdateIndexTab(index);
            this.ViewRightList(item);
        }

        private bool CheckIsCanSelectTab(int tabIndex)
        {
            var functionType = FunctionConverts.ConvertByUIHeroInfoType((UIHeroInfoTabType)tabIndex);
            return FunctionModule.CheckIsOpenAndTip(functionType);
        }

        private void ViewRightList(UIN_HeroInfoTab tab = null)
        {
            if (this.currRight != null)
                this.currRight.Hide();
            this.currRight = null;

            if (tab == null)
                tab = this.tabs.GetTEntity(this.uiData.indexTab);
            int tabIndex = tab.tabIndex;
            UIHeroInfoTabType em = (UIHeroInfoTabType)tabIndex;
            switch (em)
            {
                case UIHeroInfoTabType.Overview:
                    this.ViewOverview();
                    break;
                case UIHeroInfoTabType.Equip:
                    this.ViewEquip().Forget();
                    break;
                case UIHeroInfoTabType.UpLevel:
                    this.ViewUpLv().Forget();
                    break;
                case UIHeroInfoTabType.UpStar:
                    this.ViewUpStar().Forget();
                    break;
                case UIHeroInfoTabType.Skill:
                    this.ViewUpSkill().Forget();
                    break;
                case UIHeroInfoTabType.FateTrajectory:
                    this.ViewFateTrajectory().Forget();
                    break;
            }
        }

        private void ViewOverview()
        {
            this.currRight = this.heroInfoOverview;
            this.heroInfoOverview.Show(this.heroData);
        }

        private async UniTaskVoid ViewUpLv()
        {
            string assetPath = APIUIHeroInfo.GetTabPrefabAssetPath(((int)UIHeroInfoTabType.UpLevel));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_HeroUpdate node = await this.InitChildNodeAsync<UIN_HeroUpdate>(assetPath, this.ui.panel);
            this.currRight = node;
            node?.Show(this.heroData);
        }

        private async UniTaskVoid ViewUpSkill()
        {
            string assetPath = APIUIHeroInfo.GetTabPrefabAssetPath(((int)UIHeroInfoTabType.Skill));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_HeroSkill node = await this.InitChildNodeAsync<UIN_HeroSkill>(assetPath, this.ui.panel);
            this.currRight = node;
            node?.Show(this.heroData);
        }

        private async UniTaskVoid ViewEquip()
        {
            string assetPath = APIUIHeroInfo.GetTabPrefabAssetPath(((int)UIHeroInfoTabType.Equip));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_HeroEquip node = await this.InitChildNodeAsync<UIN_HeroEquip>(assetPath, this.ui.panel);
            this.currRight = node;
            node?.Show(this.heroData, -1);
        }

        private async UniTaskVoid ViewUpStar()
        {
            string assetPath = APIUIHeroInfo.GetTabPrefabAssetPath(((int)UIHeroInfoTabType.UpStar));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_HeroEvolution node = await this.InitChildNodeAsync<UIN_HeroEvolution>(assetPath, this.ui.panel);
            this.currRight = node;
            node?.Show(this.heroData);
        }

        private async UniTaskVoid ViewFateTrajectory()
        {
            string assetPath = APIUIHeroInfo.GetTabPrefabAssetPath(((int)UIHeroInfoTabType.FateTrajectory));
            if (string.IsNullOrEmpty(assetPath))
                return;
            UIN_HeroFateTrajectory node = await this.InitChildNodeAsync<UIN_HeroFateTrajectory>(assetPath, this.ui.panel);
            this.currRight = node;
            node?.Show(this.heroData);
        }
    }
}
